﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Items_SearchItems : System.Web.UI.Page
{
    CommonMethods _cMethods = new CommonMethods();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCategory();
            BindSubCategory();
        }
    }
    private void BindCategory()
    {
        int companyId = 1;
        DDL_Category.DataSource = _cMethods.getCategory(companyId);
        DDL_Category.DataBind();
    }
    private void BindSubCategory()
    {
        if (DDL_Category.Items.Count > 0)
        {
            DDL_SubCategory.DataSource = _cMethods.getSubCategory(Convert.ToInt32(DDL_Category.SelectedValue));
            DDL_SubCategory.DataBind();
        }
    }
 
    protected void DDL_Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSubCategory();
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        getItems();
    }

    private void getItems()
    {
        using (POSEntities db=new POSEntities())
        {
            int subCategory = Convert.ToInt32(DDL_SubCategory.SelectedValue);
            var items = (from i in db.Items where ( i.ItemName.Contains(txtItemName.Text) && i.IsDeleted==false)
                         join m in db.ItemManufacturers on i.ManufacturerId equals m.ManufacturerId
                         join sc in db.ItemSubCategories on i.SubCategoryId equals sc.SubCategoryId
                         select new
                         {
                             i.ItemId,i.ItemCode,
                             i.ItemName,i.Description,
                             i.BarCode,i.SalePrice,i.Discount,
                             Manufacturer=m.ManufacturerName,
                             SubCategory=sc.SubCategoryName
                         }).ToList();
            gvItems.DataSource = items;
            gvItems.DataBind();
        }
    }

    protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int id=Convert.ToInt32(lbtn.CommandArgument);
        using (POSEntities db=new POSEntities())
        {
            var item = (from i in db.Items where i.ItemId == id select i).FirstOrDefault();
            item.IsDeleted = true;
            db.SaveChanges();
        }
        getItems();

    }
}